<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <div class="header_search_pc hidden-xs-only">
        <el-date-picker
          v-model="dateVal"
          type="daterange"
          align="right"
          unlink-panels
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="selectDate"
        />
        <el-input v-model="listQuery.username" style="width: 120px;" placeholder="客户姓名" />
        <el-button type="primary" icon="el-icon-search" @click="handleFilter">查 找</el-button>
      </div>
      <div class="header_search_az hidden-sm-and-up">
        <el-date-picker
          v-model="dateVal"
          align="right"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 100%;"
          @change="selectDate"
        />
        <el-input v-model="listQuery.username" style=" margin: 10px 0;" placeholder="客户姓名" />
        <el-button type="primary" style="width: 100%;" icon="el-icon-search" @click="handleFilter">搜 索</el-button>
      </div>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" width="80px" label="门店编号" prop="shopId"/>
      <el-table-column align="center" min-width="120px" label="客户名称" prop="username"/>
      <el-table-column align="center" min-width="110px" label="客户电话" prop="mobilephone"/>
      <el-table-column align="center" min-width="200px" label="客户群体" prop="group"/>
      <el-table-column align="center" min-width="80px" label="客户类别" prop="type" />
      <el-table-column :formatter="formatDate" align="center" label="添加时间" prop="createTime"/>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { fetchList } from '@/api/user'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { getShopId } from '@/utils/auth'

export default {
  name: 'User',
  components: { Pagination },
  data() {
    return {
      dateVal: [],
      value: '',
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        shopId: getShopId(),
        page: 1,
        limit: 20,
        start: '',
        end: '',
        sort: '',
        order: '',
        username: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    selectDate(data) {
      if (data === null || data.length < 1) {
        this.listQuery.start = ''
        this.listQuery.end = ''
      } else {
        this.listQuery.start = new Date(data[0].getTime() + 8 * 60 * 60 * 1000)
        this.listQuery.end = new Date(data[1].getTime() + 32 * 60 * 60 * 1000 - 1)
      }
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.data.list
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    formatDate(row, column, cellValue) {
      var t = new Date(row.createTime)
      return t.getFullYear() + '-' + (t.getMonth() + 1) + '-' + t.getDate() + ' ' + t.getHours() + ':' +
       t.getMinutes() + ':' + t.getSeconds()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.app-container{
  .filter-container{
    .header_search_pc{
      width: 590px;
      margin: 0 auto;
    }
    .header_search_az{
      width: 100%;
    }
  }
}
</style>
